package com.yygo.webapp.controller.material;

import com.yygo.model.LoanRequest;
import com.yygo.model.User;
import com.yygo.service.LoanProductManager;
import com.yygo.service.LoanRequestManager;
import com.yygo.service.MaterialAuditManager;
import com.yygo.service.UserManager;
import com.yygo.util.DateUtil;
import com.yygo.webapp.controller.BaseFormController;
import com.yygo.webapp.controller.SearchForm;
import com.yygo.webapp.helper.ExtendedPaginatedList;
import com.yygo.webapp.helper.PaginateListFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by tiansha on 2015/10/31.
 */
@Controller
@RequestMapping("/materialSendList")
public class MaterialSendListController extends BaseFormController{

    @Autowired
    private MaterialAuditManager materialAuditManager;

    private PaginateListFactory paginateListFactory;
    @Autowired
    public void setPaginateListFactory(PaginateListFactory paginateListFactory) {
        this.paginateListFactory = paginateListFactory;
    }

    @Autowired
    private UserManager userManager;

    @RequestMapping(method = RequestMethod.GET)
    @ModelAttribute
    public Model handleRequest(@RequestParam(required = false, value = "date") Date date, @RequestParam(required = false, value = "status", defaultValue = "all") String status,
                               @RequestParam(required = false, value = "number") String number, @RequestParam(required = false, value = "name") String name, @RequestParam(required = false, value = "shipNumber") String shipNumber,
                               HttpServletRequest request, Principal principal)
            throws Exception {
        ExtendedPaginatedList paginatedList = paginateListFactory
                .getPaginatedListFromRequest(request);

        Model model = new ExtendedModelMap();

        if (date == null) {
            date = DateUtil.getDateBefore(10);
        }
        User loginUser = userManager.getUserByUsername(principal.getName());
        List<LoanRequest> list = materialAuditManager.searchSendLoanRequests(loginUser, status, date, number, name,shipNumber, paginatedList);
        model.addAttribute("loanRequestList", paginatedList);

        return model;
    }

    @ModelAttribute
    public SearchForm onForm(@RequestParam(required = false, value = "date") Date date, @RequestParam(required = false, value = "status", defaultValue = "all") String status,
                             @RequestParam(required = false, value = "number") String number, @RequestParam(required = false, value = "name") String name, @RequestParam(required = false, value = "shipNumber") String shipNumber) {
        SearchForm form = new SearchForm();
        form.setStatus(status);
        if (date == null) {
            date = DateUtil.getDateBefore(10);
        }
        form.setDate(date);
        form.setNumber(number);
        form.setName(name);
        form.setShipNumber(shipNumber);
        return form;
    }
}
